package com.amigo.online.provider.ffmpeg.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import okhttp3.*;

import java.io.File;
import java.io.IOException;
import java.util.Map;


/**
 * @author kml
 * @version 1.0
 * @className OkHttpUploadUtil
 * @description
 * @date 2019/4/11 18:20
 **/
public class OkHttpUploadUtil {


    //最好改成视频服务器内网地址
    public static final String MUSIC_URL = "http://ttvideos.fengzhide.com/";
    public static final String VIDEO_URL = "http://ttvideos.fengzhide.com/";

    public static String uploadMusic(File file) {
        return upload(file, MUSIC_URL);
    }

    public static String uploadVideo(File file) {
        return upload(file, VIDEO_URL);
    }


    public static String upload(File file, String url) {
        //最好用连接池
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("multipart/form-data");
        RequestBody body = RequestBody.create(mediaType, file);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("content-type", "multipart/form-data")
                .addHeader("ACCESS_TOKEN", "AMIGOTOKEN182E227CEAFF4F36A4956AA33B47E8A6")
                .build();
        String resUrl = "";
        try {
            Response response = client.newCall(request).execute();
            if (response.code() == 200) {
                Map map = JSON.parseObject(response.body().string(), Map.class);
                resUrl = (String) ((JSONArray) map.get("data")).get(0);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return resUrl;
    }

}
